<?php
/**
 * 产品模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */
if(!defined('IN_ORA')) {
	exit('Access Denied');
}

class pro extends mod {
	/**
	 * 架构函数
	 *
	 */
	function __construct(){
		parent::__construct();
		$active[get_urlStr()] = ' class="cur" ';
		$this->assign('active',$active);
	}

	/*** 查看产品 ***/
	function v(){
		$id = intval($GLOBALS['_param']['id']);
		if(!$this->isCached("pro-v-id-$id")){
			/** 站点信息 **/
			$this->assign('sinfo',$this->db->getOne('select * from '.tn('info').' order by Id asc limit 1'));

			/** 导航信息 **/
			$this->assign('navs',$this->db->getAll('select * from '.tn('nav').' order by nav_sort desc'));

			/** 产品信息 **/
			import("lib.cat");
			$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
			$this->assign('cates',$cates);

			$sql  = "select op.*,oc.cat_name,oc.path,ocnt.content,opa.attr from ".tn('product')." op left join ".tn('cat')." oc ";
			$sql .=" on oc.Id = op.cat_id left join ".tn('content')." ocnt on ocnt.fk_id = op.Id ";
			$sql .=" left join ".tn('pattr')." opa on opa.p_id = op.Id where ocnt.ctype = 1 and op.Id = $id";
			$pro = $this->db->getOne($sql);
			if(!$pro){
				redirect("index-index|true");//产品不存在
			}

			/** 图片内容 **/
			$pro['pics'] = $this->db->getAll("select * from ".tn('album')." where alb_type =1 and fk_id = $id order by cover desc");

			/** 属性处理 **/
			$attr = unserialize($pro['attr']);
			for ($i=0;$i<sizeof($attr['val']);$i++){
				if(preg_match('/^(http:\/\/)(.*?)\.(.*)+$/',$attr['val'][$i])){
					if(preg_match('/taobao\.com/i',$attr['val'][$i])){
						$attr['val'][$i] = "<a href='{$attr['val'][$i]}' class='tlink' target='_blank'>淘宝商城交易</a>";
					}else if(preg_match('/youa\.baidu/i',$attr['val'][$i])){
						$attr['val'][$i] = "<a href='{$attr['val'][$i]}' class='tlink' target='_blank'>百度有啊交易</a>";
					}else if(preg_match('/paipai\.com/i',$attr['val'][$i])){
						$attr['val'][$i] = "<a href='{$attr['val'][$i]}' class='tlink' target='_blank'>腾讯拍拍交易</a>";
					}else{
						$attr['val'][$i] = "<a href='{$attr['val'][$i]}' class='tlink' target='_blank'>{$attr['val'][$i]}</a>";
					}
				}
			}
			$pro['attr'] = $attr;
			if($pro['pro_tags']){
				$pro['pro_tags'] = explode(",",$pro['pro_tags']);
			}
			if($pro['pro_keywords']){
				$pro['pro_keywords'] = explode(",",$pro['pro_keywords']);
			}
			/** 提取产品视频信息 **/
			if($pro['vod_id']){
				$pro['vod'] = $this->db->getOne("select * from ".tn('vod')." where Id = ".$pro['vod_id']);
			}
			$pro['next'] = $this->db->getOne("select * from ".tn('product')." where Id > ".$pro['Id']." order by Id asc limit 1");
			$pro['prev'] = $this->db->getOne("select * from ".tn('product')." where Id < ".$pro['Id']." order by Id desc limit 1");
			$this->assign('pro',$pro);
			/** 当前位置 **/
			$local = array();
			$local[] = array('url'=>parse_uri('pro-index'),'text'=>'产品中心');
			$q = $this->db->query("select Id,cat_name from ".tn('cat')." where id in(".str_replace('-',',',$pro['path']).")");
			while ($row=$this->db->fetch_array($q)) {
				$local[] = array('url'=>parse_uri('pro-cat-id-%d',$row['Id']),'text'=>$row['cat_name']);
			}
			$local[] = array('url'=>parse_uri('pro-cat-id-%d',$pro['cat_id']),'text'=>$pro['cat_name']);
			$local[] = array('url'=>parse_uri('pro-v-id-%d',$pro['Id']),'text'=>$pro['pro_name']);
			$this->assign('local',$local);
			/** 专题标签 **/
			$this->assign('tags',$this->db->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));
			$np =$this->db->getOne("select Id,pro_name,pro_keywords from ".tn('product')." where id > $id order by Id asc limit 1");
			$this->assign('np',$np);
			$this->assign('title',array_reverse($local));
		}
		/** 浏览计数 **/
		if(!isset($_COOKIE["views"]["pro-$id"])){
			$this->db->execute("update ".tn('product')." set pro_views = pro_views+1 where id = $id");
			setcookie("views[pro-$id]", "ora",time()+3600);
		}
		$this->assign('views',$this->db->count("select pro_views from ".tn('product')." where Id = $id"));

		$this->display("pro.html");
	}

	/** 产品首页 **/
	function index(){
		$this->cat();
	}
	/** 产品分类 **/
	function cat(){
		$id = intval($GLOBALS['_param']['id']);
		$pid= intval($GLOBALS['_param']['p']);
		$pid= $pid>0?$pid:1;
		import("lib.page");
		if(!$this->isCached("pro-cat-id-$id-p-$pid")){
			/** 站点信息 **/
			$this->assign('sinfo',$this->db->getOne('select * from '.tn('info').' order by Id asc limit 1'));

			/** 导航信息 **/
			$this->assign('navs',$this->db->getAll('select * from '.tn('nav').' order by nav_sort desc'));

			/** 产品信息 **/
			import("lib.cat");
			$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
			$this->assign('cates',$cates);
			$curr_cat = $this->db->getOne("select * from ".tn('cat')." where id = $id");
			$this->assign('curr_cat',$curr_cat);
			$active["pro-cat-id-".$curr_cat['pid']] = ' class="cur" ';
			$this->assign('active',$active);

			/** 专题标签 **/
			$this->assign('tags',$this->db->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));

			/** 当前位置 **/
			$local = array();
			$local[] = array('url'=>parse_uri('pro-index'),'text'=>'产品中心');
			if($id>0){
				$q = $this->db->query("select Id,cat_name from ".tn('cat')." where id in(".str_replace('-',',',$curr_cat['path']).")");
				while ($row=$this->db->fetch_array($q)) {
					$local[] = array('url'=>parse_uri('pro-cat-id-%d',$row['Id']),'text'=>$row['cat_name']);
				}
				$local[] = array('url'=>parse_uri('pro-cat-id-%d',$curr_cat['Id']),'text'=>$curr_cat['cat_name']);
			}
			$this->assign('local',$local);
			$this->assign('title',array_reverse($local));

			/** 产品分页 **/
			$id = intval($GLOBALS['_param']['id']);
			$pid = is_numeric($GLOBALS['_param']['p'])?$GLOBALS['_param']['p']:1;
			import("lib.page");
			$psize = $GLOBALS['_param']['psize']?$GLOBALS['_param']['psize']:$GLOBALS['ora_cfg']['page_size'];
			if ($id > 0){
				$idStr = cat_lib::getCatIdStr($id);
				$total = $this->db->count("select count(*) from ".tn('product')." where pro_status > 0 and cat_id in ($idStr) ");
				$pobj = new page('p',$pid,$total,$psize);
				$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
				$sql .= " where cat_id in (".$idStr.") and  pro_status > 0 order by Id desc limit ".$pobj->sqlParam.") ";
				$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
				if($pobj->rsort){
					$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
					$sql .= " where cat_id in (".$idStr.") and  pro_status > 0 order by Id asc limit ".$pobj->sqlParam.") ";
					$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
				}
			}else{
				$total = $this->db->count("select count(*) from ".tn('product')." where pro_status > 0");
				$pobj = new page('p',$pid,$total,$psize);
				$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
				$sql .= " where   pro_status > 0 order by pro_status desc limit ".$pobj->sqlParam.") ";
				$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
				if($pobj->rsort){
					$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
					$sql .= " where  pro_status > 0 order by pro_status asc limit ".$pobj->sqlParam.") ";
					$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
				}
			}
			$this->assign("pobj",$pobj);
			$url = get_urlStr();
			$this->assign('url',preg_replace('/\-?p(\-|_|=)\d+/i',"",$url));
			$pros = $this->db->getAll($sql);
			/** 提取分类下第一个产品的详细信息作为分类SEO描述头信息 **/
			if($pros[0]){
				$content = $this->db->getOne("select content from ".tn('content')." where ctype=1 and fk_id = ".$pros[0]['Id']);
				if($content){
					$pros[0]['desc'] = msubstr($content['content'],0,150);
				}
			}
			$this->assign('pros',$pros);
		}
		$this->display("pro_cat.html");

	}
	/**
 * 产品图册
 */
	function album(){
		$t = intval($GLOBALS['_param']["t"]);
		$f = intval($GLOBALS['_param']["id"]);
		if($t<=0||$f<=0){
			redirect("index-index");
		}
		if(!$this->isCached("pro-album-t-$t-f-$f")){
			/** 站点信息 **/
			$this->assign('sinfo',$this->db->getOne('select * from '.tn('info').' order by Id asc limit 1'));
			if(intval($t) === 1){//产品
				$proinfo = $this->db->getOne("select * from ".tn('product')." where Id = $f");
				$images  = $this->db->getAll("select * from ".tn('album')." where fk_id = $f and alb_type = $t order by cover desc");
				$this->assign("proinfo",$proinfo);
				$this->assign("images",$images);
			}
		}
		$this->display("album.html");
	}

}
?>